package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleCity;
import com.pubinfo.passbook.common.entity.TRuleParam;
import com.pubinfo.passbook.common.entity.TSystemRole;
import com.pubinfo.passbook.common.mapper.TRuleCityMapper;
import com.pubinfo.passbook.common.mapper.TRuleParamMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TRuleParamService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.pubinfo.passbook.common.model.constant.RoleEnum.ADMIN;
import static com.pubinfo.passbook.common.model.constant.RoleEnum.AUDITOR;

/**
 *
 */
@Service
@Slf4j
public class TRuleParamServiceImpl extends ServiceImpl<TRuleParamMapper, TRuleParam>
    implements TRuleParamService {
  
  @Resource
  private TRuleCityMapper cityMapper;
  @Resource
  private CheckService userInfoService;
  
  @Override
  public boolean saveRuleParam(TRuleParam ruleParam, UserInfo userInfo, TSystemRole systemRole) {
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      
      if (!systemRole.getDistrictId().equals(ruleParam.getDistrictId()))
        return false;
    }
    TRuleCity tRuleCity = cityMapper.selectById(ruleParam.getDistrictId());
    if (tRuleCity != null)
      return this.baseMapper.insert(ruleParam) > 0;
    return false;
  }
  
  @Override
  public Map<String, Object> selectRuleParam(String districtId) {
    List<TRuleParam> ruleParamList = this.baseMapper.selectRuleParam(new RuleParam(districtId));
    Map<String, Object> paramMap = new HashMap<>();
    if (!CollectionUtils.isEmpty(ruleParamList)) {
      ruleParamList.stream().forEach(ruleParam -> {
        paramMap.put(ruleParam.getParamName(), ruleParam.getParamValue());
      });
    }
    return paramMap;
  }
  
  @Override
  public IPage<TRuleParam> selectRuleParam(RuleParam param, UserInfo userInfo, TSystemRole systemRole) {
    IPage<TRuleParam> iPage = page(param.getPage());
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      param.setDistrictId(systemRole.getDistrictId());
      return this.baseMapper.selectRuleParam(iPage, param);
    }
    if (userInfoService.judgeRole(ADMIN.getRoleCode(), userInfo))
      return this.baseMapper.selectRuleParam(iPage, param);
    return new Page();
  }
  
  @Override
  public boolean updateRuleParam(TRuleParam ruleParam, UserInfo userInfo, TSystemRole systemRole) {
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      
      if (!systemRole.getDistrictId().equals(ruleParam.getDistrictId()))
        return false;
    }
    TRuleCity tRuleCity = cityMapper.selectById(ruleParam.getDistrictId());
    if (tRuleCity != null)
      return this.baseMapper.updateById(ruleParam) > 0;
    return false;
  }
  
  @Override
  public JsonResult<String> removeRuleParam(String id, UserInfo userInfo, TSystemRole systemRole) {
    TRuleParam tRuleParam = this.baseMapper.selectById(id);
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      
      if (!systemRole.getDistrictId().equals(tRuleParam.getDistrictId()))
        return JsonResult.error("只能删除本区县的参数集");
    }
    this.baseMapper.deleteById(id);
    return JsonResult.success("删除成功");
  }
}




